package com.xqs.server.common.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.apache.log4j.Logger;

public class SerializeUtil {

	private transient static final Logger log = Logger.getLogger(SerializeUtil.class);
	
	/**
	 * 序列化 对象
	 * @author tianya
	 * @param o
	 * @return
	 */
	public static byte[] serialize(Object o){
		
		byte[] byteArray = null ;
		
		try(ByteArrayOutputStream bty = new ByteArrayOutputStream();
				ObjectOutputStream oos = new ObjectOutputStream(bty);){
			oos.writeObject(o);
			byteArray = bty.toByteArray();
		} catch (Exception e) {
			log.error("序列化失败！",e);
		}
		
		return byteArray ;
	}
	
	
	
	/**
	 * 反序列化 对象
	 * @author tianya
	 * @param bytes
	 * @return
	 */
	public Object unserialize(byte[] bytes){
		
		Object o = null ;
		
		try(ByteArrayInputStream bai = new ByteArrayInputStream(bytes);
				ObjectInputStream ois =	new ObjectInputStream(bai);){
			
			o = ois.readObject();
			
		} catch (Exception e) {
			log.error("反序列化失败！",e);
		}
		
		return o;
	}
	
	
	
	
	
	
}
